package com.ftdigital.primemovers_operators.fragment;

import android.app.AlertDialog;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.ftdigital.primemovers_operators.MainActivity;
import com.ftdigital.primemovers_operators.MyApplication;
import com.ftdigital.primemovers_operators.R;
import com.ftdigital.primemovers_operators.adpter.TextListAdpter;
import com.ftdigital.primemovers_operators.config.GlobalData;
import com.ftdigital.primemovers_operators.config.RequestAddr;
import com.ftdigital.primemovers_operators.model.AddOilInfo;
import com.ftdigital.primemovers_operators.util.MethedUtil;
import com.ftdigital.primemovers_operators.util.http.AjaxCallBack;
import com.google.gson.JsonSyntaxException;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * 无手机验证
 */
@EFragment(R.layout.fragment_no_mobile_validation)
public class NoMobileValidationFragment extends BaseFragment {
    @ViewById(R.id.back_btn)
    ImageButton backBtn;

    @ViewById(R.id.toptitle_title)
    TextView topTitle;

    @ViewById(R.id.licence_addr)
    TextView licenceAddr;

    @ViewById(R.id.licence_num)
    EditText licenceNum;

    @AfterViews
    @Override
    void initView() {
        backBtn.setVisibility(View.VISIBLE);
        topTitle.setText(getResources().getString(R.string.title_no_mobile_validate));
        licenceAddr.setText(licenseArea);
    }

    @Click({R.id.back_btn, R.id.licence_layout, R.id.submit})
    @Override
    void Click(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                MainActivity.getInstance().getSupportFragmentManager().popBackStack();
                break;
            case R.id.licence_layout:
                showSelect();
                break;
            case R.id.submit:
                if (licenceNum.getText().equals("")){
                    Toast.makeText(getActivity(),"请输入车牌", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (MethedUtil.isCheckedSpeChar(getActivity(), licenceNum.getText().toString())){
                    return;
                }
                loadData();
                break;
        }
    }

    private void loadData() {
        try {
            requestParams = "car_num=" + licenceAddr.getText().toString().trim() + licenceNum.getText().toString().trim()
                    + "&gasstation_id=" + MyApplication.userInfo.getData().getGasstation_id()
                    + "&supplier_id=" + MyApplication.userInfo.getData().getSupplier_id();
            requestUrl = RequestAddr.addoilInfo + requestParams;
        } catch (Exception e) {
            e.printStackTrace();
        }
        xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onSuccess(Object object) {
                super.onSuccess(object);
                dealData(object);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

    AddOilInfo addOilInfo;

    private void dealData(Object object) {
        try {
            addOilInfo = gson.fromJson(object.toString(), AddOilInfo.class);
        } catch (JsonSyntaxException e) {
            Toast.makeText(getActivity(), R.string.info_empty, Toast.LENGTH_SHORT).show();
            return;
        }

        if (addOilInfo.getData().getList().size() == 0) {
//            Toast.makeText(getActivity(), "车牌号不正确，请重新确认", Toast.LENGTH_SHORT).show();
            showInfo("车牌号不正确，请重新确认");
            return;
        } else {
            MainActivity.getInstance().switchFragment(this, InfoEnsureFragment_.builder()
                    .addOilInfo(addOilInfo)
                    .build());
        }
    }

    AlertDialog alertDialog;
    String licenseArea = "京";


    private void showSelect() {
        alertDialog = new AlertDialog.Builder(MainActivity.getInstance()).create();
        alertDialog.show();
        Window window = alertDialog.getWindow();
        window.setContentView(R.layout.listview_dialog);
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        ListView listView = (ListView) window.findViewById(R.id.listView);
        listView.setAdapter(new TextListAdpter(getActivity(), GlobalData.licenceNum));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                licenseArea = GlobalData.licenceNum[position];
                licenceAddr.setText(GlobalData.licenceNum[position]);
                alertDialog.dismiss();
            }
        });
    }

    AlertDialog infoDialog;

    private void showInfo(String infoContentStr) {
        infoDialog = new AlertDialog.Builder(MainActivity.getInstance()).create();
        infoDialog.show();
        Window window = infoDialog.getWindow();
        window.setContentView(R.layout.noticy_sure_dialog);
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        TextView sureInfo = (TextView) window.findViewById(R.id.sureinfo);
        TextView infoContent = (TextView) window.findViewById(R.id.info_content);
        sureInfo.setText("确定");
        infoContent.setText(infoContentStr);
//        backBtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                infoDialog.dismiss();
//            }
//        });
        sureInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                infoDialog.dismiss();
            }
        });
    }
}
